package org.pinda.network.browser;

import java.io.DataOutputStream;
import java.io.IOException;

import org.pinda.gui.MultiplayerBrowserGui;
import org.pinda.network.PacketUtils;
import org.pinda.network.ServerInfo;
import org.pinda.network.util.Stream;

public class PacketRequestServersResponse extends IBrowserPacket {

	private ServerInfo server;
	
	public PacketRequestServersResponse() {
		
	}
	
	public PacketRequestServersResponse(ServerInfo server) {
		this.server = server;
	}
	
	@Override
	public void write(DataOutputStream outStream) throws IOException {
		outStream.write(server.getIP());
		outStream.writeShort(server.getPort());
		PacketUtils.writeString(outStream, server.getRoomName());
		outStream.writeByte(server.getCurrentPlayerCount());
		outStream.writeByte(server.getMaxPlayerCount());
	}

	@Override
	public void read(Stream inStream) throws IOException {
		byte[] ip = inStream.readBytes(4);
		short port = (short)inStream.readShort();
		String roomName = PacketUtils.readString(inStream);
		byte currentPlayerCount = (byte)inStream.readByte();
		byte maxPlayerCount = (byte)inStream.readByte();
		server = new ServerInfo(ip, port, roomName, currentPlayerCount, maxPlayerCount);
	}
	
	@Override
	public void processClient(MultiplayerBrowserGui browserGui) {
		browserGui.addServer(server);
	}

	@Override
	public short getId() {
		return 3;
	}

	@Override
	public int getLength() {
		return server.getLength();
	}

}
